package com.syni.mdd.yhd.common.all.listen.order;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.dto.reponseDto.order.OrderRefundDetailRpDto;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuyUse;
import com.syni.mdd.yhd.common.all.entity.BmsDxNews;
import com.syni.mdd.yhd.common.all.entity.BmsDxOrder;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import com.syni.mdd.yhd.common.all.service.BmsDxNewsService;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.yhd.common.all.utils.ParamData;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
public class OrderRefundListener implements ApplicationListener<OrderEvent> {
    private final static String MODEL_SUCESS = "orderRefund_success";
    private final static String MODEL_ING = "orderRefund_ing";
    private final static String MODEL_FAIL = "orderRefund_fail";

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    BmsDxNewsService bmsDxNewsService;

    @Autowired
    BmsDxOrderService bmsDxOrderService;

    @Async
    public void onApplicationEvent(OrderEvent orderEvent) {
        String model = orderEvent.getSource().toString();
        if(model.indexOf("orderRefund") == -1 && !model.equals(OrderRefundCompleteListener.MODEL_COMPLETE)){
            return;
        }
        //发送给用户
        BmsDxOrder bmsDxOrder = orderEvent.getBmsDxOrder();
        Map<String,Object> userDataMap = orderEvent.getUserDataMap();
        List<BmsDxGroupBuyUse> bmsDxGroupBuyUses = (List<BmsDxGroupBuyUse>) userDataMap.get("bmsDxGroupBuyUses");
        BmsDxGroupBuyUse bmsDxGroupBuyUse = bmsDxGroupBuyUses.get(0);
        Map<String,Object> conditonMap = new HashMap<>();
        conditonMap.put("bmsOrderId",bmsDxOrder.getId());
        conditonMap.put("parentType",Constants.PARENTTYPT_CON);
        conditonMap.put("childType",Constants.PARENTTYPT_CON_GROUP_RUFUND);
        conditonMap.put("originRole",Constants.GENERAL_USER_ROLE_ID);
        conditonMap.put("bmsUserId",bmsDxOrder.getBmsUserId());
        Set<Integer> buyIds = bmsDxGroupBuyUses.stream().map(BmsDxGroupBuyUse::getId).collect(Collectors.toSet());
        conditonMap.put("bmsGroupBuyUseId_IN",buyIds);
        BmsDxNews userNew;
        List<BmsDxNews> bmsDxNews  = bmsDxNewsService.getBaseDao().findAll(new SpecificationUtils(conditonMap));
        if(!(bmsDxNews != null && bmsDxNews.size() > 0)){
            userNew = new BmsDxNews();
            userNew.setIsDelete(Constants.COMMON_FLAG_FALSE_INT);
            userNew.setBmsUserId(bmsDxOrder.getBmsUserId());
            userNew.setBmsOrderId(bmsDxOrder.getId());
            userNew.setBmsGroupBuyUseId(bmsDxGroupBuyUse.getId());
            userNew.setParentType(Constants.PARENTTYPT_CON);
            userNew.setChildType(Constants.PARENTTYPT_CON_GROUP_RUFUND);
            userNew.setTitile(bmsDxOrder.getVendorName());
            userNew.setOriginRole(Constants.GENERAL_USER_ROLE_ID);
        }else {
            userNew = bmsDxNews.get(0);
        }
        Map<String,Object> contentMap = new HashMap<>();
        contentMap.put("refundNum",Integer.parseInt(userDataMap.get("refundNum").toString()));
        contentMap.put("groupName",bmsDxOrder.getBmsDxGroupBuy().getGroupName());
        contentMap.put("refundTotal",userDataMap.get("refundTotal").toString());
        userNew.setContent(JSON.toJSONString(contentMap));
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(AuroraPushCode.USER_REFUND).getAlert(),bmsDxGroupBuyUses.size(),
                bmsDxOrder.getVendorName(),bmsDxOrder.getBmsDxGroupBuy().getGroupName(),userDataMap.get("refundTotal").toString());
        userNew.setStatus(Constants.REFUND_SUCCESS);
        if(MODEL_FAIL.equals(model)){
            userNew.setStatus(Constants.REFUND_FAIL);
            alert = String.format(AuroraPushCode.pushTemplateImproveMap.get(AuroraPushCode.USER_REFUND).get("refundFail"),bmsDxGroupBuyUses.size(),
                    bmsDxOrder.getVendorName(),bmsDxOrder.getBmsDxGroupBuy().getGroupName(),userDataMap.get("refundTotal").toString());
        }else if(MODEL_ING.equals(model)){
            userNew.setStatus(Constants.REFUND_IMG);
            alert = String.format(AuroraPushCode.pushTemplateImproveMap.get(AuroraPushCode.USER_REFUND).get("refundIng"),bmsDxGroupBuyUses.size(),
                    bmsDxOrder.getVendorName(),bmsDxOrder.getBmsDxGroupBuy().getGroupName(),userDataMap.get("refundTotal").toString());
        }
        bmsDxNewsService.save(userNew);
        BmsCommonService.pushToFront(userNew,AuroraPushCode.USER_REFUND,alert);
    }
}
